/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

package test;

import java.util.*;
import java.lang.*;
import java.io.IOException;
import bgtp.*;

//тестирование хттп транспорта
public class OhClientTest
{
    public static boolean cmpTreeMaps(TreeMap<String, String> a, TreeMap<String, String> b)
    {
	for(String key_a : a.keySet())
	    {
		if(key_a.equals("ReqType") == true | key_a.equals("Content-Length") == true)
		    {
			continue;
		    }

		if(b.containsKey(key_a) == false)
		    {
			System.out.println("\tKey "+key_a+" not in either maps");
			Helper.printSet(a.keySet(), "\ta");
			Helper.printSet(b.keySet(), "\tb");
			return false;
		    }
		
		if(a.get(key_a).equals(b.get(key_a)) == false)
		    {
			System.out.println("\ta:"+a.get(key_a));
			System.out.println("\tb:"+b.get(key_a));
			return false;
		    }
	    }
	return true;
    }

    public static void mainTest(OverHTTP oh, TreeMap<String, String> request) throws IOException
    {
	TreeMap<String, String> response;
	System.out.println("\tSimple interchanghe:");
	for(int i = 0; i < 10; i++)
	    {
		request.put("requestNo", ""+i);
		oh.send(request);
		response = oh.recv();
		System.out.println("\tSend #"+i+" is "+(cmpTreeMaps(request, response) & cmpTreeMaps(response, request)));
	    }
	
	System.out.println("\n\tMassive send(async work):");
	for(int i = 0; i < 10; i++)
	    {
		request.put("requestNo", ""+i);
		oh.send(request);
	    }
	
	for(int i = 0; i < 10; i++)
	    {
		response = oh.recv();
		System.out.println("\tRecv #"+i+" is "+(cmpTreeMaps(request, response) & cmpTreeMaps(response, request)));
	    }
    }

    public static void main(String[] args) throws IOException
    {
	TreeMap<String, String> request = new TreeMap<String, String>();

	request.put("ReqType", "GET / HTTP/1.1");
	request.put("Host", "www.google.ru");
	request.put("Accept-Encoding", "gzip,deflate");
	request.put("Keep-Alive", "115");
	request.put("Connection", "keep-alive");

	try
	    {
		TCPTransport		tcp = new TCPTransport("127.0.0.1", "1880");
		TCPTransport.TCPClient	clt = tcp.getClient();
		OverHTTP		oh  = new OverHTTP(clt);

		System.out.println("Simple headers, without body");
		mainTest(oh, request);

		request.put("Body", "very very short and simple string");
		System.out.println("\nSimple headers,with small body");
		mainTest(oh, request);

		request.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		request.put("Accept-Language", "ru,en-us;q=0.7,en;q=0.3");
		request.put("Accept-Charset", "KOI8-R,utf-8;q=0.7,*;q=0.7");
		request.put("User-Agent", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");
		request.put("User-Agent1", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");
		request.put("User-Agent2", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");
		request.put("User-Agent3", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");
		request.put("User-Agent4", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");
		request.put("User-Agent5", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");
		request.put("User-Agent6", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");
		request.put("User-Agent7", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");
		request.put("User-Agent8", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");
		request.put("User-Agent9", 
			    "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.3) Gecko/20100416 Mandriva Linux/1.9.2.3-0.2mdv2009.1 (2009.1) Firefox/3.6.3");

		System.out.println("\nBig header,with small body");
		mainTest(oh, request);

		clt.close();
	    }
	catch(IOException ioe)
	    {
		ioe.printStackTrace();
	    }
    }
}